package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.CustomLinearLayout;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.ARecomdList;
import com.sctvcloud.dongqu.beans.FProgramme;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.ui.adapter.CityStaHomepageAdapter;
import com.sctvcloud.dongqu.ui.util.RBaseItemDecoration;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.utils.TitleUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 回顾页面
 */
public class ReviewActivity extends BaseActivity implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener
        , CanRefreshLayout.IScrollView {
    @BindView(R.id.item_anchor_column_img)
    protected CustomEXImageView img;
    @BindView(R.id.item_anchor_column_title)
    protected CustomFontTextView title;
//    @BindView(R.id.item_anchor_column_lable)
//    protected CustomFontTextView lable;
    @BindView(R.id.item_anchor_column_playTime)
    protected CustomFontTextView playTime;
    @BindView(R.id.item_anchor_column_intro)
    protected CustomFontTextView intro;
    @BindView(R.id.item_anchor_column_update)
    protected CustomFontTextView update;
    @BindView(R.id.review_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected CustomLinearLayout review_ll;
    @BindView(R.id.review_list)
    protected RecyclerView review_list;
    @BindView(R.id.review_divider)
    protected View divider;
    @BindView(R.id.column_ll)
    protected LinearLayout column_ll;
    private LinearLayoutManager manager;
    private CityStaHomepageAdapter adapter;
    private String reviewUrl;
    private int currentPage = 0;
    private int pageAll = 0;
    private NewsItem newsItem;
    private String showStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_review);
        ButterKnife.bind(this);
    }

    private void initView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        review_list.setItemAnimator(new DefaultItemAnimator());
        review_list.addItemDecoration(new RBaseItemDecoration(2));
        review_list.setLayoutManager(manager);
        reviewUrl = getIntent().getStringExtra(BaseActivity.EX_ID);
        review_ll.setiScrollView(this);
        refreshLayout.autoRefresh();
    }

    @OnClick(value = {R.id.item_anchor_column_ll})
    public void playClick(View view) {
        Intent intent = new Intent(this, NewesDetailActivity.class);
        if (newsItem != null) {
            intent.putExtra(BaseActivity.EX_URL, newsItem.getNewsUrl());
            startActivity(intent);
        } else {
            toast("数据错误");
        }

    }

    @Override
    public void onLoadMore() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.loadMoreComplete();
        } else {
            currentPage = currentPage + 1;
            if (currentPage < pageAll) {
                getRecommList(currentPage);
            }
        }
//        else {
//            toast(getString(R.string.loading_no_more_data));
//            refreshLayout.loadMoreComplete();
//            refreshLayout.setLoadMoreEnabled(false);
//        }
    }

    @Override
    public void onRefresh() {
        currentPage = 0;
        getRecommList(currentPage);
    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.loadMoreComplete();
        }
        refreshLayout.setLoadMoreEnabled(currentPage + 1 < pageAll);
    }

    private void getRecommList(int calledPage) {
        if (TextUtils.isEmpty(reviewUrl)) {
            toast("数据错误");
            return;
        }
        NetUtils.getNetAdapter().getRecommList(getOwnerName(), reviewUrl, calledPage, new AbsNetCallBack<ARecomdList>(ARecomdList.class, calledPage) {
            @Override
            public void onSuc(ARecomdList aRecomdList) {
                if (aRecomdList != null) {
                    pageAll = aRecomdList.getPageAll();
                    currentPage = int1;
                    setRecomData(aRecomdList);
                }
            }

            @Override
            public void onEnd() {
                super.onEnd();
                onNetFinish();
            }
        });

    }

    public void setRecomData(ARecomdList recomData) {

        FProgramme programmeInfo = recomData.getProgrammeInfo();
        if (programmeInfo != null) {
            column_ll.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(recomData.getProgrammeInfo().getProgramImage())) {
                GlideUtil.getGlideWith34DefNormal(this, recomData.getProgrammeInfo()
                        .getProgramImage())
                        // .transform(new GlideRoundTransformUtil(this, 5))
                        .into(img);
            } else {
                img.setImageResource(R.mipmap.icon_def_3_4);
            }
            String proTitle = recomData.getProgrammeInfo().getProgramTitle();
            String proLable = recomData.getProgrammeInfo().getProgramType();
            String total = recomData.getProgrammeInfo().getProgramTotal();
            String amout = recomData.getProgrammeInfo().getPlayAmount();
            String proIntro = recomData.getProgrammeInfo().getProgramIntro();
            String proUpdate = recomData.getProgrammeInfo().getUpdateInfo();
            newsItem = recomData.getProgrammeInfo().getProgramme();
//            if (!TextUtils.isEmpty(proLable)) {
//                lable.setVisibility(View.VISIBLE);
//                lable.setText(proLable);
//            }
            if (!TextUtils.isEmpty(proTitle)) {
                title.setText(proTitle);
            } else {
                title.setText("");
            }
            if (!TextUtils.isEmpty(total) && !TextUtils.equals("0", total)) {
                showStr = "总" + total + "期";
            }
            if (!TextUtils.isEmpty(amout) && !TextUtils.equals("0", amout)) {
                showStr = "播放" + amout + "次";
            }
            if (!TextUtils.isEmpty(total) && !TextUtils.equals("0", total) && !TextUtils.isEmpty(amout) && !TextUtils.equals("0", amout)) {
                showStr = "总" + total + "期" + "|" + "播放" + amout + "次";
            }
            playTime.setText(showStr);
            if (!TextUtils.isEmpty(proIntro)) {
                intro.setText(proIntro);
            } else {
                intro.setText("");
            }
            if (!TextUtils.isEmpty(proUpdate)) {
                update.setText("更新至:" + proUpdate);
            } else {
                update.setText("");
            }
        }
        List<NewsItem> list = recomData.getList();
        if (ListUtils.isListValued(list)) {
            divider.setVisibility(View.VISIBLE);
            if (currentPage == 0) {
                if (adapter == null) {
                    adapter = new CityStaHomepageAdapter(this, list);
                    review_list.setAdapter(adapter);
                } else {
                    adapter.setData(list);
                }
            } else {
                adapter.addDatas(list);
            }

        }
    }

    @Override
    public boolean canScrollUp(MotionEvent motionEvent) {
        return ViewCompat.canScrollVertically(review_list, -1);
    }

    @Override
    public boolean canScrollDown(MotionEvent motionEvent) {
        return ViewCompat.canScrollVertically(review_list, 1);
    }
}
